package topN;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;

public class TotalPriceMapper extends Mapper<LongWritable, Text, DoubleWritable, Text> {

    private static final int TOTAL_PRICE_INDEX = 15;

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        if (line.isEmpty()) return;

        String[] fields = line.split(",");

        if (fields.length > TOTAL_PRICE_INDEX) {
            try {
                double totalPrice = Double.parseDouble(fields[TOTAL_PRICE_INDEX].trim());
                context.write(new DoubleWritable(totalPrice), new Text(line));
            } catch (NumberFormatException e) {
                System.err.println("解析数据时出错，位于行数为: " + line);
            }
        }
    }
}
